<?php

namespace app\stock\controller;

use think\Controller;

class Index extends Controller
{
  public function _initialize()
  {
    parent::_initialize();
    $this->AdminModel = model('Admin');
    $this->BusinessModel = model('Business.Business');
    $this->OrderModel = model('Product.Order');
    $this->VisitModel = model('Business.Visit');
    $this->ReceiveModel = model('Business.Receive');
    $this->SourceModel = model('Business.Source');
    //时间
    $this->TimeList = [];
    // 当前年份
    $year = date('Y'); // 2024
    // 时间封装
    for ($i = 1; $i <= 12; $i++) {
      $time = strtotime($year . "-" . $i);

      //获取每个月的第一天
      $start = date("Y-m-01", $time);

      // 获取每个月的最后一天
      $end = date("Y-m-t", $time);

      $this->TimeList[] = [strtotime($start), strtotime($end)];
    }
  }

  // 统计总数
  public function total()
  {
    $order = $this->OrderModel->count();

    $amount = $this->OrderModel->sum('amount');

    $business =  $this->BusinessModel->count();

    $data = [
      'order' => $order,
      'amount' => $amount,
      'business' => $business
    ];

    $this->success('', '', $data);
  }

  // 客户分析
  public function business()
  {
    foreach ($this->TimeList as $item) {
      // 已认证
      $certified[] = $this->BusinessModel->where(['auth' => '1',])->whereTime('createtime', 'between', $item)->count();

      // 未认证
      $nocertified[] = $this->BusinessModel->where(['auth' => '0',])->whereTime('createtime', 'between', $item)->count();
    }

    $data = [
      'certified' => $certified,
      'nocertified' => $nocertified
    ];

    $this->success('', '', $data);
  }

  // 回访分析
  public function visit()
  {
    foreach ($this->TimeList as $item) {
      $data[] = $this->VisitModel->whereTime('createtime', 'between', $item)->count();
    }

    $this->success('', '', $data);
  }

  // 领取分析
  public function receive()
  {
    foreach ($this->TimeList as $item) {
      // 申请
      $apply[] = $this->ReceiveModel->where(['status' => 'apply',])->whereTime('applytime', 'between', $item)->count();

      // 分配
      $allot[] = $this->ReceiveModel->where(['status' => 'allot',])->whereTime('applytime', 'between', $item)->count();

      //回收
      $recovery[] = $this->ReceiveModel->where(['status' => 'recovery',])->whereTime('applytime', 'between', $item)->count();
    }

    $data = [
      'apply' => $apply,
      'allot' => $allot,
      'recovery' => $recovery
    ];

    $this->success('', '', $data);
  }

  // 订单分析
  public function order()
  {
    $data = [];

    $count = $this->OrderModel->where('status', 1)->count();

    $data[] = [
      'name' => "已支付",
      'value' => $count
    ];

    $count = $this->OrderModel->where('status', 2)->count();

    $data[] = [
      'name' => "已发货",
      'value' => $count
    ];

    $count = $this->OrderModel->where('status', 3)->count();

    $data[] = [
      'name' => "已收货",
      'value' => $count
    ];

    $count = $this->OrderModel->where('status', 4)->count();

    $data[] = [
      'name' => "已完成",
      'value' => $count
    ];


    $this->success('', '', $data);
  }

  // 来源分析
  public function source()
  {
    $sourcelist = $this->SourceModel->select();

    $data = [];

    foreach ($sourcelist as $item) {

      $count = $this->BusinessModel->where('sourceid', $item['id'])->count();

      $data[] = [
        'name' => $item['name'],
        'value' => $count
      ];
    }

    $this->success('', '', $data);
  }
}
